public class LeetCode733 {

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        dfs(image, sr, sc, image[sr][sc], newColor);

        return image;
    }

    private void dfs(int[][] image, int sr, int sc,int color, int newColor){
        if (image[sr][sc] == color  && color != newColor){
            image[sr][sc] = newColor;
            System.out.println( sr + " " + sc + " " + color + " " + newColor);

            if (sr - 1 >= 0){
                dfs(image, sr - 1, sc, color, newColor);
            }

            if (sr + 1 < image.length){
                dfs(image, sr + 1, sc, color, newColor);
            }

            if (sc - 1 >= 0){
                dfs(image, sr, sc - 1, color, newColor);
            }

            if (sc + 1 < image[0].length){
                dfs(image, sr, sc + 1, color, newColor);
            }
        }
    }

    public static void main(String[] args) {
        LeetCode733 leetCode733 = new LeetCode733();
//        int[][] image = new int[][]{{1,1,1}, {1,1,0}, {1,0,1}};
        int[][] image = new int[][]{{0,0,0}, {0,1,1}};
        leetCode733.floodFill(image, 1, 1, 1);

        for (int y = 0; y < image.length; y++){
            for (int x = 0; x <image[0].length; x++){
                System.out.print(image[y][x] +" ");
            }
            System.out.println();
        }
    }
}
